// pages/user/help/index.js
const app = getApp()
import __config from '../../../config/env'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '0',
    fileList: [],
    myFeedBack: [],
    commonFeedBack: [],
    txt: '',
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.showLoading({
      title: '数据加载中',
    })
    app.api.myFeedback().then(res => {
      this.setData({
        myFeedBack: res.data
      })
      wx.hideLoading()
    })
    app.api.commonFeedback().then(res => {
      this.setData({
        commonFeedBack: res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTabsChange(e) {
    this.setData({value: e.detail.value})
    if (e.detail.value == '0') {
      app.api.myFeedback().then(res => {
        this.setData({
          myFeedBack: res.data
        })
      })
    } else if (e.detail.value == '1') {
      app.api.commonFeedback().then(res => {
        this.setData({
          commonFeedBack: res.data
        })
      })
    }
  },
  handleAdd(e) {
    const {fileList} = this.data;
    const {files} = e.detail;

    // 方法1：选择完所有图片之后，统一上传，因此选择完就直接展示
    // this.setData({
    //     fileList: [...fileList, ...files], // 此时设置了 fileList 之后才会展示选择的图片
    // });

    // 方法2：每次选择图片都上传，展示每次上传图片的进度
    files.forEach(file => this.onUpload(file))
  },
  onUpload(file) {
    const {fileList} = this.data;

    this.setData({
      fileList: [...fileList, {...file, status: 'loading'}],
    });
    const {length} = fileList;

    const task = wx.uploadFile({
      url: __config.basePath + '/common/upload',
      filePath: file.url,
      header: {
        "Content-Type": "multipart/form-data"
      },
      name: 'file',
      formData: {user: 'test'},
      success: (res) => {
        console.log(res);
        if (res.data) {
          let data = res.data;
          const json = JSON.parse(data);
          if (json.code == 200) {
            this.setData({
              [`fileList[${length}].status`]: 'done',
            });
            this.setData({
              [`fileList[${length}].url`]: json.url,
            });
          } else {
            const {fileList} = this.data;
            fileList.splice(length, 1);
            this.setData({
              fileList,
            });
          }
        } else {
          const {fileList} = this.data;
          fileList.splice(length, 1);
          this.setData({
            fileList,
          });
        }
      },
    });
    task.onProgressUpdate((res) => {
      this.setData({
        [`fileList[${length}].percent`]: res.progress,
      });
    });
  },
  handleRemove(e) {
    const {index} = e.detail;
    const {fileList} = this.data;

    fileList.splice(index, 1);
    this.setData({
      fileList,
    });
  },
  radioChange(e) {
    this.setData({
      type: e.detail.value
    })
  },
  changeContext(e) {
    this.setData({
      txt: e.detail.value
    })
  },
  submit() {
    let url = ''
    this.data.fileList.forEach(item => {
      url += item.url + ";"
    })
    let data = {}
    data.feedImg = url
    data.type = this.data.type
    data.feedBack = this.data.txt.trim()
    if (data.type == null || data.type == '') {
      wx.showToast({
        title: '请选择反馈类型',
      })
      return
    }
    if (data.feedBack == null || data.feedBack == '') {
      wx.showToast({
        title: '请输入反馈的问题',
      })
      return
    }
    app.api.addFeedback(data).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '反馈成功',
        })
      }
    })
  }
})